// pages/Pictures/Pictures.js
const app = getApp();
const utils = require('../../utils/util.js');
const post = utils.post;
const formatTime = utils.formatTime;
const jumpWeapp = utils.appid;
const getCurrentPageUrlWithArgs = utils.getCurrentPageUrlWithArgs;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tempFilePaths: [],
        mWidth: '260',
        ifimg: true,
        optionsL: '',
        btnimg: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;
        var tf = that.data.tempFilePaths.length;
        var mWidth = tf * 260;
        if (tf > 0) {
            that.setData({
                ifimg: true,
                mWidth: mWidth,
                optionsL: options,
            })
        } else {
            that.setData({
                ifimg: false,
                optionsL: options,
            })
        }
        that.pullList()
    },
    pullList() {
        let that = this;
        var time = Date.now()
        time = parseInt(time / 1000)
        var m_uid = wx.getStorageSync("m_uid");
        var tf = that.data.tempFilePaths.length;
        let p = {
            type: "find",
            m_uid: m_uid,
            weappid: that.data.optionsL.weappid,
        };
        // console.log(p)
        post(p).then(res => {
            if (-1 != res.r) {
                if (tf >= 0) {
                    that.setData({
                        tempFilePaths: res,
                        ifimg: true,
                    })
                } else {
                    that.setData({
                        ifimg: false,
                    })
                }
            }
        })
    },
    upload() {
        let that = this;
        var tf = that.data.tempFilePaths.length;
        var sl = tf - 9
            // console.log(-sl)
            // if (0 < -sl) { //限制总数九张
        if (true) {
            wx.chooseImage({
                count: -sl, // 默认9
                sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
                success: res => {
                    var tempFilesSize = res.tempFiles[0].size; //获取图片的大小，单位B
                    let tempFilePaths = res.tempFilePaths;
                    var pullImg = that.data.tempFilePaths
                    if (tempFilesSize <= 2 * 1024 * 1024) {
                        wx.showToast({
                            title: '正在上传...',
                            icon: 'loading',
                            mask: true,
                            duration: 1000
                        });
                        for (var i = 0; i < tempFilePaths.length; i++) {
                            that.ulFile(tempFilePaths[i])
                            pullImg.push(tempFilePaths[i])
                        }
                        that.setData({
                            tempFilePaths: pullImg,
                        });
                    } else { //图片大于2M，弹出一个提示框
                        wx.showToast({
                            title: '上传图片不能大于2M!',
                            icon: 'none'
                        })
                    }
                }
            })
        } else {
            wx.showToast({
                title: '最多上传9张',
                icon: 'succes',
                duration: 1000,
                mask: true
            });
        }
    },
    ulFile(u) {
        // console.log(u)
        let that = this;
        var time = Date.now()
        time = parseInt(time / 1000)
        var m_uid = wx.getStorageSync("m_uid");
        let data = {
            type: 'upload',
            upload_time: time,
            m_uid: m_uid,
            weappid: that.data.optionsL.weappid
        };
        // console.log(data)
        wx.uploadFile({
            url: utils.domain + '/review_image_upload.php',
            filePath: u,
            formData: data,
            name: 'file',
            success(res) {},
            complete(complete) {
                // console.log(complete)
                if (complete.statusCode == 200) {
                    wx.showModal({
                        title: '提示',
                        content: '提交成功!',
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '提交失败,请重新提交!',
                    })
                }
            }
        })
    },
    removeImg(e) {
        let that = this;
        // console.log(e.currentTarget.dataset.index);
        // wx.showModal({
        //     title: '提示',
        //     content: '确认要删除？',
        //     success: function(res) {
        //         if (res.confirm) {
        //             var index = e.currentTarget.dataset.index;
        //             var evalList = that.data.tempFilePaths;
        //             evalList.splice(index, 1);
        //             console.log(evalList)
        //             that.setData({
        //                 tempFilePaths: evalList,
        //             })
        //         } else if (res.cancel) {
        //         }
        //     }
        // })
        var current = e.target.dataset.src;
        wx.previewImage({
            current: current, // 当前显示图片的http链接
            urls: this.data.tempFilePaths // 需要预览的图片http链接列表
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        app.getLbs();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    /*onShareAppMessage: function() {

    }*/
})